package com.easyadmin.pro.common.context;

import com.easyadmin.pro.modules.sys.entity.UserEntity;
import com.easyadmin.pro.modules.sys.entity.UserRoleEntity;
import com.easyadmin.pro.modules.sys.vo.UserVo;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

/**
 * packageName com.easyadmin.pro.common.context
 *
 * @author 骑着蚂蚁去上天
 * @version JDK 17
 * @className UserContext
 * @date 2024/4/22
 * @description 获取当前登录用户信息
 */
@Component
public class UserContext {

    /**
     * 初始化上下文
     * @return
     */
    private Authentication getCurrentUser() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    /**
     * 获取用户信息
     * @return
     */
    public UserEntity getCurrentInfo() {
        UserVo user = (UserVo) getCurrentUser().getPrincipal();
        return user.getUserEntity();
    }

    /**
     * 获取用户名
     * @return
     */
    public String getCurrentUserName() {
        return getCurrentInfo().getUserName();
    }

    /**
     * 获取用户Id
     * @return
     */
    public Long getCurrentUserId() {
        return getCurrentInfo().getId();
    }

    /**
     * 获取用户真实姓名
     * @return
     */
    public String getCurrentTrueName() {
        return getCurrentInfo().getTrueName();
    }

    /**
     * 获取用户角色
     * @return
     */
    public UserRoleEntity getCurrentRoleInfo() {
        UserVo user = (UserVo) getCurrentUser().getPrincipal();
        return user.getUserRole();
    }

}